home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGSCAL / TINYPASC.LZH / TUDBUG.PAS < prev    next >
Pascal/Delphi Source File  |  1986-02-17  |  5KB  |  202 lines

  1.   { TUDBUG:  Skeleton file debugging routines. }
  2.   { Copyright (C) 1986 by QCAD Systems Inc., All Rights Reserved. }
  3.  
  4.   {******************}
  5.   procedure WRSYMBOL(var SYM: symbol);
  6.     { write out a symbol name. }
  7.   begin
  8.     write(rfile, sym);
  9.     end;
  10.  
  11.   {******************}
  12.   function WRTOK(TX: int): int;
  13.     { writes the print name of the TX'th token, returning
  14.       the number of characters output. }
  15.     var TL: int;
  16.   begin
  17.     tx := tokx[tx];
  18.     tl := 0;
  19.     while tokchar[tx] <> chr(0) do begin
  20.       write(rfile, tokchar[tx]);
  21.       tx := tx+1;
  22.       tl := tl+1
  23.       end;
  24.     wrtok := tl;
  25.     end;
  26.  
  27.   {****************}
  28.   procedure WRPROD(PRX: int);
  29.     { write out the PRX'th production (a series of tokens). }
  30.     var TL: int;
  31.   begin
  32.     prx := prodx[prx];
  33.     tl := wrtok(prods[prx]);
  34.     write(rfile, ' ->');
  35.     prx := prx+1;
  36.     while prods[prx]<>0 do begin
  37.       write(rfile, ' ');
  38.       tl := wrtok(prods[prx]);
  39.       prx := prx+1;
  40.       end
  41.     end;
  42.  
  43.   {******************}
  44.   procedure DUMP_SYM(INDENT: int; SYMP: symtabp;
  45.                      NTAG: string31);
  46.     { output information on the given symbol table entry.  this can
  47.       be extended to handle user-defined symbol types (e.g. functions
  48.       and variables). }
  49.   begin
  50.     if symp<>nil then
  51.     with symp^ do begin
  52.       writeln(rfile);
  53.       write(rfile, ' ':indent, ntag, ': ');
  54.       wrsymbol(sym);
  55.       write(rfile, ' (', sym_names[symt], ')');
  56.       case symt of
  57.         var_type: write(rfile, ' VADDR=', vaddr:1);
  58.         func_type:
  59.           write(rfile, ' FADDR=', faddr:1, ' PBYTES=', pbytes:1,
  60.                        ' IS_ACTUAL=', is_actual,
  61.                        ' IS_SYSTEM=', is_system);
  62.         ELSE ;
  63.         end
  64.       end
  65.     end;
  66.   
  67.   {*****************}
  68.   procedure DUMP_SEM(INDENT: int; SEMSTK: semrecp;
  69.                      NTAG: string31);
  70.     { output a semantic stack record. }
  71.   begin
  72.     if semstk<>nil then
  73.     with semstk^ do begin
  74.       writeln(rfile);
  75.       write(rfile, ' ':indent);
  76.       write(rfile, sem_names[semt], ': ');
  77.       case semt of
  78.         other:  ;
  79.         ident:  dump_sym(indent+2, symp, 'symp');
  80.         fixed:  write(rfile, numval:1);
  81.         ELSE  write(rfile, ' ... user form')
  82.         end
  83.       end
  84.     end;
  85.  
  86.   {*********************}
  87.   procedure STK_DUMP(KIND: string8;  var STACK: state_stack;
  88.                      STACKX: int;  CSTATE: int);
  89.     { produce a symbolic dump of the parser stack. }
  90.     var SX, TL, LL: int;
  91.   begin
  92.     if debug>2 then begin
  93.       write(rfile, kind {, ', state ', cstate:1} );
  94.       if cstate>=readstate then begin
  95.         write(rfile, ', on token ');
  96.         tl := wrtok(token);
  97.         end;
  98.       writeln(rfile, ', memavail ', memavail:1);
  99.       end;
  100.     if cstate<readstate then begin
  101.       { reduce state }
  102.       if debug>1 then begin  {complete stack dump}
  103.         if tos>15 then begin
  104.           writeln(rfile, '  ###');
  105.           sx := tos-15;
  106.           end
  107.         else
  108.         sx := 1;
  109.         while sx<=tos do begin
  110.           tl:=0;
  111.           write(rfile, tos-sx:3, ': ');
  112.           tl:=tl+5;
  113.          {write(rfile, stack[sx]:3, ' ');
  114.           tl:=tl+4; }
  115.           if sx=tos then
  116.             tl := tl+wrtok(insym[cstate])
  117.           else
  118.           tl := tl+wrtok(insym[stack[sx+1]]);
  119.           dump_sem(6, sem[sx], '');
  120.           writeln(rfile);
  121.           sx:=sx+1;
  122.           end
  123.         end;
  124.       wrprod(cstate);
  125.       writeln(rfile)
  126.       end;
  127.     { don't let this roll off the top of the screen }
  128.     idebug
  129.     end;
  130.  
  131.   {****************}
  132.   procedure IDEBUG;
  133.     { interactive debugging support }
  134.     var QUIT:  boolean;
  135.  
  136.     {..................}
  137.     procedure SHOW_SYM;
  138.       label 1;
  139.       { asks for a symbol, then dumps the symbol table entry for it }
  140.       var SP:  symtabp;
  141.           LINE:  string80;
  142.           SX:  integer;
  143.     begin
  144.       1:
  145.       write('What symbol? ');
  146.       readln(line);
  147.       if length(line)>sizeof(symbol) then goto 1;
  148.       sp := findsym(symtab, line);
  149.       if sp<>nil then
  150.         dump_sym(0, sp, '')
  151.       else
  152.         writeln('Unknown symbol');
  153.       writeln;
  154.       end;
  155.  
  156.     {.................}
  157.     procedure DUMP_ALL;
  158.       { show everything in the symbol table }
  159.       var HX: int;
  160.           SP: symtabp;
  161.     begin
  162.       for hx := 0 to hlimit do begin
  163.         sp := symtab[hx];
  164.         while sp<>nil do begin
  165.           with sp^ do begin
  166.             if not (symt in [reserved, symerr]) then begin
  167.               { report only the nontrivial stuff }
  168.               wrsymbol(sym);
  169.               write(rfile, ' ');
  170.               end;
  171.             sp := next
  172.             end
  173.           end
  174.         end;
  175.       writeln(rfile);
  176.       end;
  177.  
  178.     {................}
  179.     procedure SET_DEBUG;
  180.       { prompts for a debug level number }
  181.     begin
  182.       write('Set debug level to (0, 1, ...)? ');
  183.       readln(debug);
  184.       end;
  185.  
  186.   begin { idebug }
  187.     quit := false;
  188.     while not quit do begin
  189.       writeln('Trace is ', trace);
  190.       case upcase(resp(
  191.   'I(dentifier, D(ebug level, A(ll symbols, T(race, C(ontinue? ')) of
  192.         'I':  show_sym;
  193.         'A':  dump_all;
  194.         'D':  set_debug;
  195.         'C':  quit := true;
  196.         'T':  trace := not(trace);
  197.         ELSE ;
  198.         end
  199.       end
  200.     end { idebug };
  201.  
  202.